package com.ebook.ui.fragment.home.category;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.ArrayList;
public class BookCategory extends JPanel {
    private int width;
    private int itemWidth;
    private int height;
    public BookCategory(int width,String name){
        super();
        this.setLayout(null);
        this.width = width;
        itemWidth = (width-10)/3;
        BookCategoryLink oriLink = new BookCategoryLink(name,SwingConstants.LEFT);
        oriLink.setBounds(0,0,width,50);
        oriLink.setFont(new Font("微软雅黑",Font.PLAIN,18));
        oriLink.setForeground(Color.ORANGE);
        add(oriLink);
        setBackground(Color.white);
        this.setBorder(new LineBorder(Color.lightGray));
    }
    public void loadCategory(ArrayList<String> categoryList){
        int i = 0;
        for(String category:categoryList){
            BookCategoryLink blink = new BookCategoryLink(category,SwingConstants.CENTER);
            blink.setSize(itemWidth,40);
            blink.setLocation(new Point(i*(itemWidth+5),i/3+50));
            add(blink);
            i++;
//            System.out.println(blink);
        }
        buildSize(i);
    }
    /**
     * 计算该大类所占用的高度
     * */
    public int buildSize(int nums){
        int height = calculate(nums)*40+50;
        this.setSize(this.width,height);
        this.height = height;
        return height;
    }
    public int getBuildHeight(){
        return this.height;
    }
    @Deprecated
    public int calculateYBounds(int x){
        if(x%3==0){
            return x/3-1;
        }else{
            return x/3;
        }
    }
    public int calculate(int x){
        //3个一行,返回行数
        if(x%3==0){
            return x/3;
        }else{
            return x/3+1;
        }
    }
}
